package com.fjwt.gz.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fjwt.gz.core.model.ApiRes;
import com.fjwt.gz.db.dto.PayAuthDTO;
import com.fjwt.gz.db.entity.PayAuthEntity;
import com.fjwt.gz.db.entity.SysConfig;
import com.fjwt.gz.db.vo.PayAuthVO;
import com.fjwt.gz.service.mapper.PayAuthMapper;
import com.fjwt.gz.service.mapper.SysConfigMapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.http.util.TextUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 支付通道认证配置 服务实现类
 * </p>
 *
 * @author shiyonghao
 * @since 2025-04-09
 */
@Slf4j
@Service
public class PayAuthService extends ServiceImpl<PayAuthMapper, PayAuthEntity> {

    @Autowired
    private SysConfigMapper sysConfigMapper;

    /**
     * 查询三方出资列表
     * @param iPage
     * @param payAuthDTO
     * @return
     */
    public IPage<PayAuthVO> selectPageList(IPage iPage, PayAuthDTO payAuthDTO) {
        IPage<PayAuthVO> page = baseMapper.selectPageList(iPage, payAuthDTO);
        // 数据脱敏
        page.getRecords().forEach(item-> {
            String msg=item.getAuthConfig();
            int length=msg.length();
            String startStr=msg.substring(0,length/6);
            String endStr=msg.substring(length/6*4,length);
            item.setAuthConfig(String.format("%s**************************%s",startStr,endStr));
        });
        return page;
    }

    /**
     * 新增出资方
     * @return
     */
    public void add(PayAuthDTO payAuthDTO) {
        PayAuthEntity payAuthEntity = new PayAuthEntity();
        payAuthEntity.setIfCode(payAuthDTO.getIfCode());
        payAuthEntity.setIfName(payAuthDTO.getIfName());
        payAuthEntity.setIsCommSubject(payAuthDTO.getIsCommSubject());
        payAuthEntity.setCreatedAt(new Date());

        SysConfig sysConfig = getSysConfig(payAuthDTO);
        sysConfig.setCreatedAt(new Date());

        baseMapper.insert(payAuthEntity);
        sysConfigMapper.insert(sysConfig);
    }

    /**
     * 修改出资方
     * @return
     */
    public ApiRes revamp(PayAuthDTO payAuthDTO) {
        if (payAuthDTO.getPayAuthId()==null||TextUtils.isEmpty(payAuthDTO.getIfCode())||TextUtils.isEmpty(payAuthDTO.getIfName())) {
            return ApiRes.customFail("请求参数异常，请检查！");
        }
        Long authId = payAuthDTO.getPayAuthId();
        PayAuthEntity payAuthEntity = baseMapper.selectById(authId);
        if (payAuthEntity == null) {
            throw new RuntimeException("该记录不存在！");
        }
        if (TextUtils.isEmpty(payAuthDTO.getAuthConfig())) {
            payAuthDTO.setAuthConfig(null);
        }

        payAuthEntity.setIfCode(payAuthDTO.getIfCode());
        payAuthEntity.setIfName(payAuthDTO.getIfName());
        payAuthEntity.setIsCommSubject(payAuthDTO.getIsCommSubject());
        payAuthEntity.setUpdatedAt(new Date());

        SysConfig sysConfig = getSysConfig(payAuthDTO);
        sysConfig.setUpdatedAt(new Date());

        baseMapper.updateById(payAuthEntity);
        sysConfigMapper.updateById(sysConfig);
        return ApiRes.ok();
    }

    private SysConfig getSysConfig(PayAuthDTO payAuthDTO) {
        SysConfig sysConfig = new SysConfig();
        sysConfig.setConfigKey(payAuthDTO.getIfCode());
        sysConfig.setConfigName("通道配置");
        sysConfig.setConfigDesc(payAuthDTO.getIfName());
        sysConfig.setGroupKey("payConfig");
        sysConfig.setGroupName("同主体");
        if (payAuthDTO.getIsCommSubject()==1){
            sysConfig.setGroupName("异主体");
        }
        sysConfig.setConfigVal(payAuthDTO.getAuthConfig());
        sysConfig.setSortNum(payAuthDTO.getSortNum());
        return sysConfig;
    }

    public PayAuthEntity detailByLite(Long payAuthId) {
        PayAuthEntity payAuthEntity = baseMapper.detailByLite(payAuthId);
        return payAuthEntity;
    }

    /**
     * 新增出资方
     * @param payAuthDTO
     */
    public ApiRes insert(PayAuthDTO payAuthDTO) {
        if (TextUtils.isEmpty(payAuthDTO.getIfCode())||TextUtils.isEmpty(payAuthDTO.getIfName())||TextUtils.isEmpty(payAuthDTO.getAuthConfig())) {
            return ApiRes.customFail("请求参数异常，请检查！");
        }
        return ApiRes.ok();
    }
}
